<template>
	<view class="search" :style="{'marginTop':StatusBar + 15+ 'px'}">
		<view class="search_head">
			<view class="head_back" @click="navigateBack()">
				<text class="iconfont icon-Left"></text>
			</view>
			<view class="head_input">
				<view class="input_left" @tap="show = true">
					<text>{{ game.gameName }}</text>
					<text class="iconfont icon-Bottom"></text>
				</view>
				<input type="text" v-model="title" placeholder="搜索一下吧~" @confirm="search" />
			</view>
			<view class="head_search" @click="search">
				<text class="iconfont icon-sousuo"></text>
			</view>
		</view> 
		
		<view class="search_content">
			<view class="content_history"  v-if="localSearch.length">
				<view class="history_head">
					<text class="head_title">历史记录</text>
					<text class="iconfont icon-lajixiang" @tap="delShow = true"></text>
				</view>
				<view class="history_list">
					<view class="list_item" v-for="(item,index) in localSearch" :key="index" @click="to(item)">
						{{ item.title }}
					</view>
				</view>
			</view>
			<view class="content_hot">
				<view class="hot_title">大家都在搜</view>
				<view class="hot_list">
					<view class="list_item"  :class="index < 3 ? 'current': ''" v-for="(item, index) in 8" :key="item">
						<text class="item_number">{{index + 1}}</text>
						<text class="item_name">永劫无间</text>
					</view>
				</view>
			</view>
			
			<view class="content_hot">
				<view class="hot_title">热门搜索</view>
				<view class="hot_box">
					<view class="box_item" v-for="(item, index) in 6">
						<view class="item_number">0{{index+1}}</view>
						<view class="item_image">
							<image src="../../static/logo.png" mode=""></image>
						</view>
						<view class="item_text">永劫无间</view>
					</view>
				</view>
			</view>
		</view>




		<u-picker :show="show" :columns="[gameList]" keyName="gameName" :closeOnClickOverlay="true" title="选择游戏"
			@close="cancel" @cancel="cancel" @confirm="confirm"></u-picker>
		<u-modal :show="delShow" content='确认删除全部的历史搜索记录吗？此操作将不可恢复' showCancelButton closeOnClickOverlay
			@close="delShow = false" @cancel="delShow = false" @confirm="yesDel"></u-modal>
		<u-toast ref="uToast"></u-toast>

	</view>
</template>

<script>
	// import {
	// 	listQt
	// } from "@/api";
	export default {
		data() {
			return {
				StatusBar: this.StatusBar,
				title: '',
				gameList: [],
				show: false,
				game: {}, //当前选中游戏的数据
				localSearch: [],
				delShow: false,

			}
		},
		onLoad() {
			this.getAreaDo()
		},
		onShow() {
			let localSearch = uni.getStorageSync('localSearch') || '[]'
			this.localSearch = JSON.parse(localSearch)
		},
		methods: {
			yesDel() {
				//存储
				uni.removeStorageSync('localSearch')
				this.localSearch = []
				this.delShow = false
				let params = {
					type: 'success',
					message: "删除成功!",
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
				}
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			confirm({
				value
			}) {
				this.game = value[0]
				this.cancel()
			},
			cancel() {
				this.show = false
			},
			async getAreaDo() {
				// let {
				// 	data
				// } = await listQt()
				let data = [
				{
					"id": 11,
					"gameName": "永劫无间",
					"gameIcon": "http://img.liudongyang.com/137FB16B281E-组 4@2x(3).png",
					"gameType": 1,
					"gameState": 1,
					"isHot": 1
				},
				{
					"id": 32,
					"gameName": "无畏契约",
					"gameIcon": "http://img.axyds.com/85C13CCDD180169158846396538070B9F71C7.png",
					"gameType": 1,
					"gameState": 1,
					"isHot": 1
				}]
				this.gameList = data
				this.game = data[0]

				console.log(this.gameList)
			},
			to({
				title,
				id
			}) {
				uni.navigateTo({
					url: `/pages/list/list?gameId=${id}&title=` + title
				})
			},
			search() {
				//浏览记录
				let localSearch1 = uni.getStorageSync('localSearch') || '[]'
				let localSearch = JSON.parse(localSearch1)

				if (this.title) {
					let is = true //允许添加

					for (let i = 0; i < localSearch.length; i++) {
						if (localSearch[i].title == this.title) {
							is = false
						}
					}

					if (is) {
						localSearch.unshift({
							title: this.title,
							id: this.game.id
						})
						//存储
						uni.setStorageSync('localSearch', JSON.stringify(localSearch))
					}
				}

				uni.navigateTo({
					url: `/pages/list/list?gameId=${this.game.id}&title=` + this.title
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.search_head{
		width: 750rpx;
		display: flex;
		align-items: center;
		
		.head_back {
			margin: 0 30rpx 0 40rpx;
			.iconfont {
				font-size: 32rpx;
				font-weight: bold;
				color: #09244B;
			}
		}
		
		.head_search {
			width: 100rpx;
			height: 68rpx;
			background: #191815;
			border-radius: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-left: 20rpx;
			.iconfont {
				color: #fff;
				font-size: 36rpx;
			}
		}
		
		.head_input{
			height: 72rpx;
			background: #F3F4F9;
			border-radius: 64rpx;
			display: flex;
			align-items: center;
			.input_left{
				width: 180rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				text{
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #212121;
					margin-right: 12rpx;
				}
			}
			input{
				flex: 1;
				padding:  0 24rpx;
				box-sizing: border-box;
				font-size: 24rpx;
			}
		}
		
	}
	.search_content {
		padding: 0 32rpx;
		.content_history {
			margin-top: 36rpx;
			.history_head {
				display: flex;
				align-items: center;
				.head_title {
					font-size: 28rpx;
					color: #22150B;
					font-weight: bold;
				}
				.iconfont {
					margin-left: auto;
					font-size: 32rpx;
				}
			}
			.history_list{
				box-sizing: border-box;
				display: flex;
				flex-wrap: wrap;
				.list_item {
					background: #F3F4F9;
					border-radius: 64rpx;
					padding: 10rpx 22rpx;
					font-size: 20rpx;
					color: #22150B;
					margin-right: 16rpx;
					margin-top: 16rpx;
				}
			}
		}
		.content_hot {
			margin-top: 36rpx;
			.hot_title {
				font-size: 28rpx;
				font-weight: bold;
				background: -webkit-linear-gradient(90deg, #FF1826, #FFC36A);
				-webkit-background-clip: text;
				-webkit-text-fill-color: transparent;
				display: inline-block;
			}
			.hot_list {
				margin-top: 16rpx;
				width: 100%;
				height: 240rpx;
				background: linear-gradient( 180deg, #FFE78F 0%, rgba(255,255,255,0) 100%);
				border-radius: 20rpx;
				display: inline-block;
				box-sizing: border-box;
				.list_item {
					float: left;
					width: 50%;
					padding: 12rpx 28rpx;
					box-sizing: border-box;
					display: flex;
					align-items: center;

					.item_number {
						font-size: 28rpx;
						line-height: 28rpx;
						color: #22150B;
						font-weight: bold;
						margin-right: 34rpx;
					}
					.item_name {
						font-size: 24rpx;
						font-weight: bold;
						color: #22150B;
					}
					&.current {
						.item_number {
							background: -webkit-linear-gradient(90deg, #FF1D1D, #FF902A);
							-webkit-background-clip: text;
							-webkit-text-fill-color: transparent;
						}
					}
				}
			}
			.hot_box {
				margin-top: 16rpx;
				width: 100%;
				background: linear-gradient( 180deg, #FFE78F 0%, rgba(255,255,255,0) 100%);
				border-radius: 20rpx;
				display: inline-block;
				box-sizing: border-box;
				padding: 24rpx;
				.box_item {
					display: flex;
					align-items: center;
					margin-bottom: 24rpx;
					.item_number {
						font-size: 32rpx;
						font-weight: bold;
						background: -webkit-linear-gradient(90deg, #FF1D1D, #FF902A);
						-webkit-background-clip: text;
						-webkit-text-fill-color: transparent;
						margin-right: 16rpx;
					}
					.item_image {
						width: 56rpx;
						height: 56rpx;
						border-radius: 8rpx;
						overflow: hidden;
						margin-right: 16rpx;
						image {
							width: 100%;
							height: 100%;
						}
					}
					.item_text {
						font-size: 24rpx;
						color: #22150B;
						font-weight: bold;
					}
				}
			}
		}
	}

	
	
	
</style>
